package com.daxt.controller.filemanager;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.model.constant.BaseConstants;
import com.daxt.model.dic.DicDocFileType;
import com.daxt.model.dic.DicDocState;
import com.daxt.model.search.ArchivesSearch;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.daxt.common.result.PageInfo;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.model.service.archives.result.ArchivesList;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.service.DocService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "档案检索")
@RestController
@RequestMapping("/fileManager")
public class SearchManagerController {

	@Autowired
	private DocService docService;
	@Autowired
	private OperationLogService operationLogService;

	@ApiOperation(value = "检索", notes = "")
	@GetMapping("/searchFile")
	public Result<ArchivesList> searchFile(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "fondsId", required = false) String fondsId,
			@ApiParam("") @RequestParam(value = "typeId", required = false) String typeId,
			@ApiParam("") @RequestParam(value = "classId", required = false) String classId,
			@ApiParam("条件") @RequestParam(value = "condition", required = false) String condition,
			@ApiParam("0件 1卷") @RequestParam(value = "model", required = false) String model,
			@ApiParam("关键词") @RequestParam(value = "keywords", required = false) String keywords,
			@ApiParam("部门")@RequestParam(value="deptId",required = false)String deptId
			) {
		// UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil,
		// request);
		//String state = "2";

		ArchivesSearch as = ArchivesSearch.builder().fondsId(fondsId).typeId(typeId).classId(classId)
				.condition(condition)
				.keywords(keywords)
				.fileType(DicDocFileType.DOC.getKey())
				.deptId(deptId)
				.state(DicDocState.archive.getKey())
				.isDelete(BaseConstants.DELETE_NORMAL).build();

		if ("0".equals(model)) {
			return docService.list(pageNum, pageSize, as);
		} else if ("1".equals(model)) {
			as.setFileType(DicDocFileType.DOSSIER.getKey());
			return docService.list(pageNum, pageSize, as);
		} else {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", null);
		}

	}
	
	
	@ApiOperation(value = "检索关键词", notes = "")
	@GetMapping("/searchKeywords")
	@LoginAuth()
	public Result<PageInfo<ArchivesDoc>> searchKeywords(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "fondsId", required = false) String fondsId,
			@ApiParam("") @RequestParam(value = "typeId", required = false) String typeId,
			@ApiParam("") @RequestParam(value = "classId", required = false) String classId,
			@ApiParam("条件") @RequestParam(value = "condition", required = false) String condition,
			@ApiParam("0件 1卷") @RequestParam(value = "model", required = false) String model,
			@ApiParam("关键词") @RequestParam(value = "keywords", required = false) String keywords,
			@ApiParam("0 整理  1归档审批  2 归档") @RequestParam(value = "state", required = false) String state,
			@ApiParam("条件中文") @RequestParam(value = "conditionCn", required = false) String conditionCn,
			@ApiIgnore @LoginUser UserInfo loginUser,
			HttpServletRequest request) {

		if(!"admin".equals(loginUser.getUsername())&& !"超级管理员".equals(loginUser.getRole())) {
			if(!StringUtils.isEmpty(loginUser.getFondsId())) fondsId=loginUser.getFondsId();
		}

		Result<PageInfo<ArchivesDoc>> pageList = docService.keywordsListManager(pageNum, pageSize, typeId, fondsId, classId, keywords, model, state,null);
		if(!StringUtils.isEmpty(conditionCn))operationLogService.saveLog("3", "检索"+conditionCn+"", request);
		return pageList;
	}

	/**
	 * @description: 条件选择门类
	 *
	 * @author: lig
	 * @date: 2021/7/8
	 */
	@ApiOperation(value = "检索关键词条件(门类)  通过门类名称", notes = "")
	@GetMapping("/searchKeywordsCondition")
	public Result<PageInfo<ArchivesDoc>> searchKeywordsCondition(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("门类名称") @RequestParam(value = "typeName", required = false) String typeName,
			@ApiParam("关键词") @RequestParam(value = "keywords", required = false) String keywords,
			@ApiParam("条件中文") @RequestParam(value = "conditionCn", required = false) String conditionCn,
//			@ApiParam("0 整理  1归档审批  2 归档") @RequestParam(value = "state", required = false) String state,
			HttpServletRequest request) {

		Result<PageInfo<ArchivesDoc>> pageList = docService.keywordsTypesListManager(pageNum, pageSize, typeName, keywords, null,  "2",null);
		if(!StringUtils.isEmpty(conditionCn))operationLogService.saveLog("3", "检索"+conditionCn, request);

		return pageList;
	}
	/**
	 * @description: 获取门类组
	 *
	 * @author: lig
	 * @date: 2021/7/8
	 */
	@ApiOperation(value = "获取门类组", notes = "")
	@GetMapping("/typeGroup")
	@LoginAuth()
	public Result<List<ArchivesType>> searchKeywordsCondition(@ApiIgnore @LoginUser UserInfo loginUser) {
		List<ArchivesType> list = docService.types(loginUser);

		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", list);
	}

	
	@ApiOperation(value = "检索条件", notes = "")
	@GetMapping("/searchCondition")
	public Result<PageInfo<ArchivesDoc>> searchCondition(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "fondsId", required = false) String fondsId,
			@ApiParam("") @RequestParam(value = "typeId", required = false) String typeId,
			@ApiParam("") @RequestParam(value = "classId", required = false) String classId,
			@ApiParam("条件") @RequestParam(value = "condition", required = false) String condition,
			@ApiParam("0件 1卷") @RequestParam(value = "model", required = false) String model,
			@ApiParam("0 整理  1归档审批  2 归档") @RequestParam(value = "state", required = false) String state,
			@ApiParam("条件中文") @RequestParam(value = "conditionCn", required = false) String conditionCn,
			HttpServletRequest request) {

		Result<PageInfo<ArchivesDoc>> pageList = docService.conditionListManager(pageNum, pageSize, typeId, fondsId, classId, condition, model, state);
		if(!StringUtils.isEmpty(conditionCn))operationLogService.saveLog("3", "检索"+conditionCn+"", request);
		return pageList;
	}





}
